package com.xckj.teacher.ui.fragment

import com.xckj.teacher.R
import com.xckj.teacher.bean.ClassBean
import com.xckj.teacher.databinding.FragmentClassBinding
import com.xckj.teacher.framework.BaseFragment
import com.xckj.teacher.iview.IClassView
import com.xckj.teacher.presenter.ClassPresenter
import com.xckj.teacher.ui.activity.ClassDetailActivity
import com.xckj.teacher.ui.activity.EditClassActivity
import com.xckj.teacher.ui.activity.HomeworkReportActivity
import com.xckj.teacher.ui.activity.RoomReportActivity
import com.xckj.teacher.ui.adapter.ClassListAdapter
import com.xckj.teacher.util.ActivityMessenger
import com.xckj.teacher.util.get
import com.xckj.teacher.util.startActivity

/**
 * @Describe: 班级
 */
class ClassFragment : BaseFragment<FragmentClassBinding?>() {

    private val classListAdapter: ClassListAdapter by lazy { ClassListAdapter(arrayListOf()) }
    private val presenter: ClassPresenter by lazy { ClassPresenter(this) }

    override fun isStatusBarDarkFont(): Boolean {
        return false
    }

    override fun loadLayout(): Int {
        return R.layout.fragment_class
    }

    override fun setStatusBarColor(): Int {
        return R.color.f7f8fa
    }

    override fun initData() {
        dataBinding?.smartRefresh?.autoRefresh()
        dataBinding?.smartRefresh?.setOnRefreshListener {
            presenter.classList({
                classListAdapter.setList(it)
                dataBinding?.smartRefresh?.finishRefresh()
            }, {
                dataBinding?.smartRefresh?.finishRefresh()
            })
        }
        initClassList()
    }

    private fun initClassList() {
        dataBinding?.recClassList?.setAdapter(classListAdapter)
        classListAdapter.setOnItemClickListener { adapter, view, position ->
            ActivityMessenger.startActivityForResult(
                this@ClassFragment.activity,
                ClassDetailActivity::class,
                "id" to classListAdapter.data[position].id
            ) {
                if (it?.get<String>("refresh") == "1") {
                    dataBinding?.smartRefresh?.autoRefresh()
                }
            }
        }
        classListAdapter.setOnItemChildClickListener { adapter, view, position ->
            var classBean = classListAdapter.data[position]
            when (view.id) {
                R.id.tv_report_home -> {
                    ActivityMessenger.startActivity<HomeworkReportActivity>(
                        this.requireActivity(),
                        "classId" to "${classBean.id}",
                        "className" to "${classBean.name}"
                    )
                }

                R.id.tv_report_computer -> {
                    ActivityMessenger.startActivity<RoomReportActivity>(
                        this.requireActivity(),
                        "classId" to "${classBean.id}",
                        "className" to "${classBean.name}"
                    )
                }

            }
        }
    }

}
